﻿using System.Windows;
using DevExpress.XtraGrid;
using System.Windows.Documents;
using System.Collections;
using CafeteriaLib;
using System.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Diagnostics;
using System.Threading;

namespace CafeteriaInterface
{
	/// <summary>
	/// Interaction logic for Payment.xaml
	/// </summary>
	public partial class Payment : Window
	{
		private int idOrderDto;
		private BindingList<OrderDto> listOrderDto;

		private List<ItemCategoryDto> listCategory;
		private List<Configuration> listConfiguration;

		public Payment()
		{
			InitializeComponent();
			
			Init();
		}

		public void Init()
		{
			idOrderDto = 0;
			listOrderDto = new BindingList<OrderDto>();
			listConfiguration = new List<Configuration>();

			// init data for Category
			listCategory = new List<ItemCategoryDto>();
			listCategory.Add (new ItemCategoryDto ("Coffee", "Cup", 1, 10));
			listCategory.Add (new ItemCategoryDto ("Moctail", "Glass", 1, 100));
			listCategory.Add (new ItemCategoryDto ("Pizza", "Dish", 1, 1000));

			// init data for Configuration
			listConfiguration.Add(new Configuration("LogFilePath", "D:\\Install\\AppLog"));
			listConfiguration.Add(new Configuration("LogFileName", "CafeteriaLog_*.log"));

			dxgGridPayment.ItemsSource = listOrderDto;

			cboItem.ItemsSource = listCategory;
			cboItem.DisplayMember = "Item";
			cboItem.ValueMember = "Item";
		}

		private void cboItem_EditValueChanged(object sender, DevExpress.Xpf.Editors.EditValueChangedEventArgs e)
		{
			try
			{
				// query Unit (linq)
				tbUnit.Text = listCategory.FirstOrDefault(x => x.Item == cboItem.Text).Unit;

				// query Price (linq)
				tbPrice.Text = listCategory.FirstOrDefault(x => x.Item == cboItem.Text).Price.ToString();
				
				tbQty.Text = "1";
			}
			catch(Exception)
			{
			}
		}

		private void btnAdd_Click(object sender, RoutedEventArgs e)
		{
			if (!string.IsNullOrWhiteSpace(cboItem.Text))
			{
				listOrderDto.Add(new OrderDto
				{
					Id = ++idOrderDto,
					Item = cboItem.Text,
					Unit = tbUnit.Text,
					Qty = int.Parse(tbQty.Text),
					Price = int.Parse(tbPrice.Text),
					SubTotal = int.Parse(tbQty.Text) * int.Parse(tbPrice.Text)
				});
			}

			
		}

		private void btnDelete_Click(object sender, RoutedEventArgs e)
		{

		}

		private void dxgGridPayment_SelectionChanged(object sender, DevExpress.Xpf.Grid.GridSelectionChangedEventArgs e)
		{
			int rowHandle = 0;

			if( dxgGridPayment.VisibleRowCount != 0)
			{
				rowHandle = tbviewGridPayment.FocusedRowHandle;
				if( dxgGridPayment.IsValidRowHandle(rowHandle))
				{
					cboItem.Text = Convert.ToString(dxgGridPayment.GetCellValue(rowHandle, "Item"));
					tbUnit.Text = Convert.ToString(dxgGridPayment.GetCellValue(rowHandle, "Unit"));
					tbQty.Text = Convert.ToString(dxgGridPayment.GetCellValue(rowHandle, "Qty"));
					tbPrice.Text = Convert.ToString(dxgGridPayment.GetCellValue(rowHandle, "Price"));
				}
			}
		}

		private void deleteRowItem_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
		{
			int rowHandle = 0;
			int id = 0;

			if (dxgGridPayment.VisibleRowCount != 0)
			{
				rowHandle = tbviewGridPayment.FocusedRowHandle;
				if (dxgGridPayment.IsValidRowHandle(rowHandle))
				{
					try
					{
						id = Convert.ToInt32(dxgGridPayment.GetCellValue(rowHandle, "Id"));
						listOrderDto.Remove((OrderDto) listOrderDto.First(x => x.Id == id));
					}
					catch (Exception) 
					{
						EventLog.CreateEventSource("", "");
					}
				}
			}
		}

		private void editRowItem_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
		{
			int rowHandle = 0;

			if (dxgGridPayment.VisibleRowCount != 0)
			{
				rowHandle = tbviewGridPayment.FocusedRowHandle;
				if (dxgGridPayment.IsValidRowHandle(rowHandle))
				{
					cboItem.Text = Convert.ToString(dxgGridPayment.GetCellValue(rowHandle, "Item"));
					tbUnit.Text = Convert.ToString(dxgGridPayment.GetCellValue(rowHandle, "Unit"));
					tbQty.Text = Convert.ToString(dxgGridPayment.GetCellValue(rowHandle, "Qty"));
					tbPrice.Text = Convert.ToString(dxgGridPayment.GetCellValue(rowHandle, "Price"));
				}
			}
		}
	}
}
